package com.learn.project2.controller.wx.auth;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.learn.project2.config.shiro.MarketToken;
import com.learn.project2.model.bo.wx.auth.AuthLoginBO2;
import com.learn.project2.model.bo.wx.auth.AuthRegCaptchaBO2;
import com.learn.project2.model.bo.wx.auth.AuthRegisterBO;
import com.learn.project2.model.vo.wx.auth.AuthLoginVO2;
import com.learn.project2.service.wx.auth.AuthService2;
import com.learn.project2.utils.Result;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Random;

/**
 * \* Author: Valley
 * \* Date: 2022/1/8
 * \* Time: 17:18
 * \* Description:
 */
@RestController
@RequestMapping("wx/auth")
public class AuthController2 {

    @Autowired
    AuthService2 authService2;

    String AllSTRING;


    @PostMapping ("regCaptcha")
    public Result regCaptcha(@RequestBody AuthRegCaptchaBO2 authRegCaptchaBO2 ) {
        Random random = new Random();
        String str ="";
        String result="";
        for (int i=0;i<6;i++)
        {
            str +=random.nextInt(10);
        }
        AllSTRING =str;
        String AllString="{\"code\":\""+AllSTRING+"\"}";
        String mobile = authRegCaptchaBO2.getMobile();
        int length = mobile.length();
        //传入手机号，一次调用支持发送一个手机号验证码
        String AccessKeyId = "LTAI5t8gpxPTCR6W58RnZq4u";//对应的accesskeyid
        String AccessKeySecret = "mVPbpbxia0JQotb7HyJAREV8QUuq8h";//对应的accesskeysecret
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", AccessKeyId, AccessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
            request.setMethod(MethodType.GET);
            request.setDomain("dysmsapi.aliyuncs.com");
            request.setVersion("2017-05-25");//日期别乱写
            request.setAction("SendSms");
            request.putQueryParameter("RegionId", "cn-hangzhou");
            request.putQueryParameter("PhoneNumbers",mobile );//手机号
            request.putQueryParameter("SignName", "stone4j");//签名名称
            request.putQueryParameter("TemplateCode", "SMS_173765187");//模板code
            request.putQueryParameter("TemplateParam",AllString);//这个里面填写验证码内容
        CommonResponse response = null;
        try {
            response = client.getCommonResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        try {
            System.out.println(response.getData());
        } catch (Exception e) {
            e.printStackTrace();
        }


        return Result.ok();
    }
    /**
     * @description: 注册功能
     * @param: [loginBO, session]
     * @return: com.learn.project2.utils.Result
     * @author 王力
     * @date: 2022-01-10 21:56:41
     */
    @RequestMapping("register")
    public Result register(@RequestBody AuthRegisterBO authRegisterBO,HttpServletRequest request) throws Exception {
;
        if(StringUtils.equals(AllSTRING,authRegisterBO.getCode())){
            authService2.authRegister(authRegisterBO);
            AuthLoginBO2 loginBO2 = new AuthLoginBO2();
            loginBO2.setUsername(authRegisterBO.getUsername());
            loginBO2.setPassword(authRegisterBO.getPassword());
            Subject subject = SecurityUtils.getSubject();
            MarketToken token = new MarketToken(authRegisterBO.getUsername(),authRegisterBO.getPassword(),"wx");
            subject.login(token);
            AuthLoginVO2 authLogin = authService2.authLogin(loginBO2, request);
            return Result.ok(authLogin);
        }
        return Result.fail("验证码错误");
    }

    /**
     * @description: 登录功能
     * @param: [loginBO, session]
     * @return: com.learn.project2.utils.Result
     * @author 王力
     * @date: 2022-01-10 21:56:41
     */
    @RequestMapping("login")
    public Result login(@RequestBody AuthLoginBO2 loginBO, HttpServletRequest request) throws Exception {
        Subject subject = SecurityUtils.getSubject();
        MarketToken token = new MarketToken(loginBO.getUsername(),loginBO.getPassword(),"wx");
//        UsernamePasswordToken token = new UsernamePasswordToken(loginBO.getUsername(),SecretUtils.enAESAndMD5(loginBO.getPassword()));
        subject.login(token);
        AuthLoginVO2 authLogin = authService2.authLogin(loginBO, request);
        return Result.ok(authLogin);
    }

    /**
     * @description: 权限退出
     * @param: []
     * @return: java.lang.String
     * @author 王力
     * @date: 2022-01-11 11:12:39
     */
    @RequestMapping("logout")
    public Result logout(){
        Subject subject = SecurityUtils.getSubject();
        Integer code = authService2.authLogout(subject);
        subject.logout();
        return Result.ok();
    }
}
